package com.dj.ms.configure;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.schema.ScalarType;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ParameterType;
import springfox.documentation.service.RequestParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static springfox.documentation.builders.PathSelectors.regex;


/**
 * <br>
 * <p>
 *     swagger cfg
 * </p>
 *
 * <br>
 * @author ZJG
 * @version 1.0
 * @date 2021/9/29 下午6:49
 */
@Slf4j
@Data
@Configuration
@EnableOpenApi
@ConfigurationProperties(prefix = "swagger")
public class SwaggerConfiguration {

    /**
     * swagger.basePkg
     */
    private String basePkg;
    /**
     * 服务版本
     */
    private String version = "1.0";

    @Value("${spring.application.name:DJ微服务}")
    private String proNm;
    @Value("${server.port:8080}")
    private int serverPort;

    @Bean
    @ConditionalOnMissingBean(Docket.class)
    public Docket userApi() {
        String baseName = StringUtils.isNotBlank(proNm)? proNm.toUpperCase(): "DJ微服务";

        Docket docket = new Docket(DocumentationType.OAS_30)
                .globalResponses(HttpMethod.GET, new ArrayList<>())
                .globalResponses(HttpMethod.PUT, new ArrayList<>())
                .globalResponses(HttpMethod.POST, new ArrayList<>())
                .globalResponses(HttpMethod.DELETE, new ArrayList<>())
                .enable(true)
                .groupName(baseName)
                .apiInfo(apiInfo())
                .select()
                    .apis(StringUtils.isNotBlank(basePkg)? RequestHandlerSelectors.basePackage(basePkg): RequestHandlerSelectors.any())
                    .paths(regex( ".*"))
                    .build();
        return docket;
    }

    private ApiInfo apiInfo() {
        String baseName = StringUtils.isNotBlank(proNm)? proNm.toUpperCase() + "服务": "德基微服务";
        return new ApiInfoBuilder()
                .title(baseName)
                .description(baseName.concat("相关接口"))
                .contact(new Contact("ZJG", "", ""))
                .license( "Apache 2.0")
                .licenseUrl( "http://www.apache.org/licenses/LICENSE-2.0")
                .version(version)
                .build();
    }


    private List<RequestParameter> globalRequestParameters() {
        RequestParameterBuilder parameterBuilder = new RequestParameterBuilder()
                .in(ParameterType.HEADER)
                .name("Authorization")
                .required(false)
                .query(param -> param.model(model -> model.scalarModel(ScalarType.STRING)));
        return Collections.singletonList(parameterBuilder.build());
    }












}
